<template>
	<view>
		<uv-status-bar />
		<g-tabs :tabs="tabs" :active="active" status-bar @change="changeTab" />
		<Arrange v-if="active === 0" :refresh="refresh" :onMore="onMore" @onRefresh="(v) => emit('update:refresh', v)"
			@onReach="(v) => emit('update:onMore', v)" />
		<Data-board v-else-if="active === 1" :refresh="refresh" :onMore="onMore"
			@onRefresh="(v) => emit('update:refresh', v)" @onReach="(v) => emit('update:onMore', v)" />
		<Untreated v-else-if="active === 2" :refresh="refresh" :status-bar="false" @count="handleCount"
			@onRefresh="(v) => emit('update:refresh', v)" />
	</view>
</template>
<script setup>
	import {
		ref,
		onMounted
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		getStatistics
	} from "@/request/home";
	import Arrange from "@/pages/Home/cp/Arrange.vue";
	import DataBoard from "@/pages/Home/cp/DataBoard.vue";
	import Untreated from "@/pages/Home/cp/Untreated.vue";

	const props = defineProps({
		// 是否下拉刷新
		refresh: {
			type: Boolean,
			default: false,
		},
		// 上拉加载
		onMore: {
			type: Boolean,
			default: false,
		},
	});

	const emit = defineEmits(["update:refresh", "update:onMore", "count"]);

	const active = ref(-1); // tab下标
	const tabs = ref([{
			label: "日程安排",
			badge: {
				value: 0
			},
		},
		{
			label: "数据看板",
			badge: {
				value: 0
			},
		},
		{
			label: "待办事项",
			badge: {
				value: 0
			},
		},
	]);

	onLoad((e) => {
		setTimeout(() => {
			if ("type" in e) {
				active.value = Number(e.type);
			} else {
				active.value = 0;
			}
		}, 0);
	});

	onMounted(() => {
		getUnTaskCount();
	});

	// tab切换
	const changeTab = (val) => {
		active.value = val.index;
		uni.stopPullDownRefresh();
	};

	// 统计数量
	const handleCount = (val) => {
		const index = tabs.value.findIndex((i) => i.label == val.label);
		if (index != -1) {
			tabs.value[index].badge.value = val.count;
		}
	};

	// 获取待办任务数量
	const getUnTaskCount = () => {
		getStatistics()
			.then((res) => {
				if (res.code == 200) {
					const {
						reportNum,
						handleWorkOrderNum
					} = res.data;
					tabs.value[2].badge.value = reportNum + handleWorkOrderNum;
					uni.$emit("untreated", reportNum + handleWorkOrderNum);
				}
			});
	};
</script>
<style lang="scss" scoped></style>